package com.uvnos4j.matilda.schedule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uvnos4j.matilda.commons.annotation.AppLog;
import com.uvnos4j.matilda.commons.dto.GridTable;
import com.uvnos4j.matilda.commons.dto.PageParam;
import com.uvnos4j.matilda.commons.dto.QueryParam;
import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.schedule.domain.ScheduleTask;
import com.uvnos4j.matilda.schedule.service.ScheduleTaskService;
import com.uvnos4j.matilda.schedule.vo.ScheduleTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 调度任务 Controller
 * <p>
 * Description:调度任务 Controller
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @see com.uvnos4j.matilda.schedule.controller
 * @since 2020-08-31 22:42:44
 */
@Controller
@RequestMapping("/schedule/task")
public class ScheduleTaskController {

    @Autowired
    private ScheduleTaskService scheduleTaskService;

    @PreAuthorize("hasAuthority('schedule:task')")
    @AppLog(title = "调度任务管理")
    @GetMapping("/list")
    public String index() {
        return "schedule/scheduletask";
    }

    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @GetMapping
    public Result findPage() {
        // 组装分页信息
        PageParam pageParam = RequestUtil.packagePageParamByNumAndSize("page", "rows");
        // 组装排序信息
        String orderBy = RequestUtil.packageOrderBy("sort", "order", true);
        // 得到所有的查询参数，注意也包括分页信息和排序信息
        QueryParam param = RequestUtil.getQueryParam();

        IPage<ScheduleTask> page = scheduleTaskService.findPage(param, pageParam, orderBy);
        GridTable<ScheduleTask> table = new GridTable<>(page.getTotal(), page.getRecords());

        return Result.success(table);
    }

    @AppLog(title = "添加调度任务")
    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @PostMapping
    public Result add(@Valid ScheduleTaskVo vo) {
        ScheduleTask task = vo.buildModel(ScheduleTask.class, false);
        boolean b = scheduleTaskService.save(task);
        if (b) {
            return Result.success("定时任务添加成功", task.getId());
        }
        return Result.fail("定时任务添加失败");
    }

    @AppLog(title = "修改调度任务")
    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @PutMapping
    public Result update(@Valid ScheduleTaskVo vo) {
        ScheduleTask task = vo.buildModel(ScheduleTask.class, true);
        boolean b = scheduleTaskService.updateById(task);
        if (b) {
            return Result.success("定时任务修改成功", task.getId());
        }
        return Result.fail("定时任务修改失败");
    }

    @AppLog(title = "删除调度任务")
    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<String> idList) {
        boolean b = scheduleTaskService.removeByIds(idList);
        if (b) {
            return Result.success("定时任务删除成功,删除[" + idList.size() + "]条记录");
        }
        return Result.fail("定时任务删除失败");
    }

    /**
     * 根据任务key => 启动任务
     */
    @AppLog(title = "启动调度任务")
    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @PostMapping("/start")
    public Result start(@RequestParam("taskKey") String taskKey) {
        scheduleTaskService.start(taskKey);
        return Result.success("启动任务成功");
    }

    /**
     * 根据任务key => 停止任务
     */
    @AppLog(title = "停止调度任务")
    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @PostMapping("/stop")
    public Result stop(@RequestParam("taskKey") String taskKey) {
        scheduleTaskService.stop(taskKey);
        return Result.success("停止任务成功");
    }

    /**
     * 根据任务key => 重启任务
     */
    @AppLog(title = "重启调度任务")
    @PreAuthorize("hasAuthority('schedule:task')")
    @ResponseBody
    @PostMapping("/restart")
    public Result restart(@RequestParam("taskKey") String taskKey) {
        scheduleTaskService.restart(taskKey);
        return Result.success("重启任务成功");
    }

}